package com.yanyilu.qiubite.jpa;

import com.yanyilu.qiubite.entity.Part;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface PartJpa extends JpaRepository<Part,Integer> {
    @Query(value = "select * from part where story_id = :storyId",nativeQuery = true)
    List<Part> findByStoryId(@Param("storyId") Integer storyId);

    @Query(value = "select part_id from part where story_id = :storyId",nativeQuery = true)
    List<Integer> findPartIdByStoryId(@Param("storyId")Integer storyId);

    @Modifying
    @Query(value = "delete from part where story_id = :storyId",nativeQuery = true)
    void deleteByStoryId(@Param("storyId")Integer storyId);

    @Modifying
    @Query(value = "delete from part where story_id in :storyIds",nativeQuery = true)
    void deleteByStoryIds(@Param("storyIds")List<Integer> storyIds);

    @Query(value = "select part_id from part where story_id in :storyIds",nativeQuery = true)
    List<Integer> selectPartIdsByStoryIds(@Param("storyIds")List<Integer> storyIds);

    @Query(value = "select * from part where story_id in :storyIds and if(:partName is not null,part_name = :partName,1=1)",nativeQuery = true)
    Page<Part> findAllByStoryIds(Pageable pageable, @Param("partName") String partName, @Param("storyIds") List<Integer> storyIds);

    @Query(value = "select * from part where story_id = :storyId",nativeQuery = true)
    Page<Part> findPageByStoryId(@Param("storyId")Integer storyId, Pageable pageable);
}
